package xyz.luhui1hao.sell.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import xyz.luhui1hao.sell.dto.OrderDTO;
import xyz.luhui1hao.sell.enums.ResultEnum;
import xyz.luhui1hao.sell.exception.SellException;
import xyz.luhui1hao.sell.form.OrderForm;
import xyz.luhui1hao.sell.service.BuyerService;
import xyz.luhui1hao.sell.service.OrderService;
import xyz.luhui1hao.sell.utils.ResultVOUtil;
import xyz.luhui1hao.sell.vo.ResultVO;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static xyz.luhui1hao.sell.converter.OrderForm2OrderDTOConverter.convert;
import static xyz.luhui1hao.sell.enums.ResultEnum.PARAM_ERROR;

/**
 * @ClassName BuyerOrderController
 * @Description TODO
 * @Author luhui1hao
 * @Date 2018/8/30 下午10:08
 * @Version 1.0
 */
@RestController
@RequestMapping("buyer/order")
@Slf4j
public class BuyerOrderController {

    @Autowired
    private OrderService orderService;
    @Autowired
    private BuyerService buyerService;

    //创建订单
    @PostMapping("/create")
    public ResultVO<Map<String, String>> create(@Valid OrderForm orderForm, BindingResult bindingResult){
        if(bindingResult.hasErrors()){
            log.error("【创建订单】参数不正确，orderForm = {}", orderForm);
            throw new SellException(PARAM_ERROR.getCode(), bindingResult.getFieldError().getDefaultMessage());
        }

        OrderDTO orderDTO = new OrderDTO();
        orderDTO = convert(orderForm);
        if(CollectionUtils.isEmpty(orderDTO.getOrderDetailList())){
            log.error("【创建订单】购物车不能为空");
            throw new SellException(ResultEnum.CART_EMPTY);
        }

        Map<String, String> map = new HashMap<>();
        OrderDTO createResult = orderService.createOrder(orderDTO);
        map.put("orderId", createResult.getOrderId());

        return ResultVOUtil.success(map);
    }
    //订单列表
    @PostMapping("/list")
    public ResultVO<List<OrderDTO>> list(@RequestParam("openid") String openid,
                                         @RequestParam(value = "page", defaultValue = "1") Integer page,
                                         @RequestParam(value = "size", defaultValue = "10") Integer size){
        if(StringUtils.isEmpty(openid)){
            log.error("【查询订单列表】openid为空");
            throw new SellException(ResultEnum.PARAM_ERROR);
        }

        List<OrderDTO> orderDTOList = orderService.findList(openid, page, size);

        return ResultVOUtil.success(orderDTOList);
    }

    //订单详情
    @PostMapping("/detail")
    public ResultVO<OrderDTO> detail(@RequestParam("openid") String openid,
                                     @RequestParam("orderId") String orderId){
        OrderDTO orderDTO = buyerService.findOrderOne(openid, orderId);

        return ResultVOUtil.success(orderDTO);
    }

    //取消订单
    @PostMapping("/cancel")
    public ResultVO<OrderDTO> cancel(@RequestParam("openid") String openid,
                                     @RequestParam("orderId") String orderId){
        buyerService.cancel(openid, orderId);

        return ResultVOUtil.success();
    }

}
